﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.EntitiesInfo;
using System.Data;
using ConvertUtility = Module.Core.LoginSystem.Commons.Utilities.ConvertUtility;
using MiscUtility = Module.Core.LoginSystem.Commons.Utilities.MiscUtility;

namespace Web.Cms.Advertistment
{
    public partial class addEditBanner : AdminPage
    {
        int advId;
       private AdvertisementInfo advertisementInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(Server.MapPath("/Uploads/Banner/" + CurrentUser.UserID)))
                Directory.CreateDirectory(Server.MapPath("/Uploads/Banner/" + CurrentUser.UserID));
            fpMedia.fpUploadDir = "/Uploads/Banner/" + CurrentUser.UserID;
            advId = ConvertUtility.ToInt32(Request.QueryString["advertisementID"]);
            if (!IsPostBack)
            {
                BannerPos.Fill(dropPriority);
               
                ObjMiscUtility.FillIndex(dropPriority, 1, 100, 0);
                AdvertisementPositionsBL.FillDropList(dropGroupBanner, "0");
                //dropGroupBanner.DataSource = AdvertisementPositionsBL.SelectAll();
                //dropGroupBanner.DataTextField = "Title";
                //dropGroupBanner.DataValueField = "PositionID";
                //dropGroupBanner.DataBind();
               LoadData();
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AdvertisementInfo adv = new AdvertisementInfo();
                if (adv == null) return;

                if (DateTime.Compare(rcldEndDate.SelectedDate, rcldStartDate.SelectedDate) < 0)
                {
                    trShowWarning.Visible = true;
                    rcldEndDate.Focus();
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                    return;
                }
                else
                {
                    trShowWarning.Visible = false;
                }
                FillData(adv);
                int advId = AdvertisementBL.Insert(adv);

                lblUpdateStatus.Text = "Thêm mới thành công";

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                 advertisementInfo = new AdvertisementInfo();
                 FillData(advertisementInfo);
                if (txtName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtName.Focus();
                    return;
                }
                if (txtDescription.Text.Length == 0)
                {
                    MessageBox.Show("Bạn chưa nhập tên mô tả?");
                    txtDescription.Focus();
                    return;
                }
                else
                {
                    AdvertisementBL.Update(advertisementInfo);
                    HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=ListAdvertismentPublic';</script>");
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        private void FillData(AdvertisementInfo adv)
        {
            adv.AdvertisementID = advId;
            adv.Title = txtName.Text;
            adv.Description = txtDescription.Text;
            adv.MediaSrc = fpMedia.Text;
            adv.Width = 0;
            adv.Height = 0;
            adv.MediaType = ConvertUtility.ToInt32(dropMediaType.SelectedValue);
            adv.Target = txtLink.Text;
            adv.Priority = ConvertUtility.ToInt32(dropPriority.SelectedValue);
            adv.CreatedBy = CurrentUser.UserID;
            adv.CreatedOn = DateTime.Now;
            adv.StartDate = ConvertUtility.ToDateTime(rcldStartDate.SelectedDate);
            adv.EndDate = ConvertUtility.ToDateTime(rcldEndDate.SelectedDate);
            adv.CustomerID = 0;
            adv.Click = 0;
            adv.ViewCount = 0;
            if (chkEnable.Checked)
            {
                adv.IsEnable = 1;
            }
            else
            {
                adv.IsEnable = 2;
            }
            adv.PositionID = Convert.ToInt32(dropGroupBanner.SelectedValue);
        }

        public void LoadData()
        {
            try
            {
                if (advId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    chkEnable.Visible = false;

                    advertisementInfo = AdvertisementBL.GetInfo(2,advId.ToString(),"");
                    if (advertisementInfo != null)
                    {
                        txtID.Text = advertisementInfo.AdvertisementID.ToString();
                        txtName.Text = advertisementInfo.Title;
                        txtDescription.Text = advertisementInfo.Description;
                        txtLink.Text = advertisementInfo.Target;
                        fpMedia.Text = advertisementInfo.MediaSrc;
                        TxtCustomer.Text = advertisementInfo.CustomerID.ToString();
                        rcldStartDate.SelectedDate = advertisementInfo.StartDate;
                        rcldEndDate.SelectedDate = advertisementInfo.EndDate;
                        txtClick.Text = advertisementInfo.Click.ToString();
                        dropGroupBanner.SelectedValue = advertisementInfo.PositionID.ToString();
                        if (advertisementInfo.IsEnable == 1)
                        {
                            chkEnable.Checked = true;
                        }
                        else
                        {
                            chkEnable.Checked = false;
                        }
                        txtViewCount.Text = advertisementInfo.ViewCount.ToString();
                        litMedia.Visible = true;
                        dropMediaType.SelectedValue = advertisementInfo.MediaType.ToString();
                        MiscUtility.SetSelected(dropPriority, advertisementInfo.Priority.ToString());
                        lblUpdateStatus.Visible = false;
                        btnAdd.Visible = false;
                        btnUpdate.Visible = true;



                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;


                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }

        protected void btnCanel_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=ListAdvertismentPublic';</script>");
        }
    }
}